﻿
namespace TextCompare
{
    class CharTextMatcher : ITextMatcher
    {
        public CharTextMatcher(string source, string target)
        {
            this.Source = source.Replace("\r\n", "\n");
            this.Target = target.Replace("\r\n", "\n");

            SourceCount = string.IsNullOrWhiteSpace(this.Source) ? 0 : this.Source.Length;
            TargetCount = string.IsNullOrWhiteSpace(this.Source) ? 0 : this.Target.Length;
        }

        public string Source
        {
            get;
            private set;
        }

        public string Target
        {
            get;
            private set;
        }

        public int SourceCount
        {
            get;
            private set;
        }

        public int TargetCount
        {
            get;
            private set;
        }

        public virtual bool Compare(int i, int j)
        {
            return Source[i].Equals(Target[j]);
        }

        public virtual string GetSourceSubString(int startIndex, int endIndex)
        {
            return SubString(Source, startIndex, endIndex);
        }

        public virtual string GetTargetSubString(int startIndex, int endIndex)
        {
            return SubString(Target, startIndex, endIndex);
        }

        private string SubString(string str, int start, int end)
        {
            return str.Substring(start, end - start + 1);
        }
    }
}
